package com.spit.fammo.ui.frg

import android.os.Bundle
import android.view.MenuItem
import androidx.core.view.GravityCompat
import com.blankj.utilcode.util.CrashUtils.init
import com.spit.fammo.ext.showLoading
import com.spit.fammo.viewmodel.MainModel
import com.google.android.material.navigation.NavigationView
import com.kingja.loadsir.core.LoadService
import com.spit.fammo.R
import com.spit.fammo.databinding.FMainBinding
import com.spit.amoeba.base.BaseFragment

/**
 * @Author nike
 * @Date 2023/7/7 11:59
 * @Description
 */
class MainFrg: BaseFragment<MainModel, FMainBinding>(), NavigationView.OnNavigationItemSelectedListener {

    //界面状态管理者
    lateinit var loadsir: LoadService<Any>


    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.navView.setNavigationItemSelectedListener(this)

        setHasOptionsMenu(true)

        mDatabind.includeToolbar.toolbar.run {
            init(getString(R.string.app_name))
            inflateMenu(R.menu.home_menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.home_set -> {
                        onOpenDrawer()
                    }
                }
                true
            }
        }


    }

    override fun createObserver() {
        super.createObserver()

    }


    override fun lazyLoadData() {
        //设置界面 加载中
        loadsir.showLoading()
        mViewModel.onRequest(false)
    }

    override fun onDestroy() {
        super.onDestroy()
        mActivity.setSupportActionBar(null)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        mDatabind.drawerLayout.postDelayed({
            when(item.itemId){

            }
            mDatabind.drawerLayout.closeDrawer(GravityCompat.END)
        }, 300)
        return true
    }

    //region  抽屉布局
    fun onOpenDrawer() {
        if (!mDatabind.drawerLayout.isDrawerOpen(GravityCompat.END)) {
            mDatabind.drawerLayout.openDrawer(GravityCompat.END)
        }
    }

}